package IOoperation;



import java.io.*;
import java.util.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import com.thoughtworks.xstream.*;
import core.Coordinate;
import factory.ComponentFactory;
import factory.ComponentInterface;
import factory.CreatComponent;

public class XMLcontroler {
	private List<ComponentInterface> ComponentList;
	private List<ComponentInterface> listcomp = new ArrayList<ComponentInterface>();
	private ComponentInterface component;

	public XMLcontroler() {
		ComponentList = new ArrayList<ComponentInterface>();
		ComponentFactory comp = new CreatComponent();
		ComponentInterface earth = comp.creatEarth();
		earth.setPosition(new Coordinate(1, 2));
		ComponentInterface water = comp.creatWater();
		ComponentList.add(earth);
		ComponentList.add(water);
		exportToXml();
		importFromXml();
		ListIterator li = ComponentList.listIterator();
		while (li.hasNext()) {
			System.out.println(li.next());
		}
	}

	private void exportToXml() {
		XStream xs = new XStream();
		try {
			FileWriter xmlFile = new FileWriter("xmles.xml");
			xmlFile.write("<?xml version=\"1.0\"?>\n");
			xmlFile.write("<Components>\n");
			xmlFile.close();
			FileOutputStream fs = new FileOutputStream("xmles.xml", true);
			ListIterator li = ComponentList.listIterator();
			while (li.hasNext()) {
				xs.toXML(li.next(), fs);
			}
			fs.close();
			xmlFile = new FileWriter("xmles.xml", true);
			xmlFile.write("\n</Components>");
			xmlFile.close();
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}

	private void importFromXml() {
		/*
		 * XStream xs = new XStream(); try { FileInputStream fis = new
		 * FileInputStream("xmles.xml"); while (xs.fromXML(fis, component) !=
		 * null) { listcomp.add(component); } } catch (FileNotFoundException ex)
		 * { ex.printStackTrace(); } return listcomp;
		 */
		try {
			BufferedReader bf = new BufferedReader(new InputStreamReader(
					System.in));
			File file = new File("xmles.xml");
			if (file.exists()) {
				// Create a factory
				DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
				// Use the factory to create a builder
				DocumentBuilder builder = factory.newDocumentBuilder();
				Document doc = builder.parse("xmles.xml");
				// Get a list of all elements in the document
				NodeList list = doc.getElementsByTagName("factory.components.Water");
				System.out.println("XML Elements: ");
				System.out.println(list.getLength());
				for (int i = 0; i < list.getLength(); i++) {
					// Get element
					Node fstNode = list.item(i);

				//	if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
						System.out.println("ifbe vagyok!!!!:P");
						Element fstElmnt = (Element) fstNode;
						NodeList nameLst = fstElmnt
								.getElementsByTagName("name");
						Element fstNmElmnt = (Element) nameLst.item(0);
						NodeList nameNm = fstNmElmnt.getChildNodes();
						System.out.println("Name : "
								+ ((Node) nameNm.item(0)).getNodeValue());
						NodeList lstNmElmntLst = fstElmnt
								.getElementsByTagName("lastname");
						Element lstNmElmnt = (Element) lstNmElmntLst.item(0);
						NodeList lstNm = lstNmElmnt.getChildNodes();
						System.out.println("Last Name : "
								+ ((Node) lstNm.item(0)).getNodeValue());
				//	}
				//	System.out.println(element.getNodeValue());
			}
				
			} else {
				System.out.print("File not found!");
			}
		} catch (Exception e) {
			System.exit(1);
		}
	}

	public static void main(String[] args) {
		XMLcontroler xc = new XMLcontroler();
	}
}